import {Button, Table, message, Popconfirm} from 'antd';
import React, {Component} from "react";
import {DownloadOutlined, WarningOutlined} from "@ant-design/icons";
import {Resultlist, ResultDownload, ResultData, resultDelete} from "@/services/A10/ResultController"
import styles from "@/pages/Model/model.less";

class ExamplePage extends Component{

  state = {
    resultlist: [],
    listcolumns: [],
    resultcolumns:[],
    Resultdata:[],
    pageSize: 50
  }
  async componentDidMount() {
    const resultlist = await Resultlist();
    const listcolumns = [
      {
        title: '分类结果列表',
        dataIndex: 'name',
        key: 'name',
        render: (text) => <a>{text}</a>,
      },
      {
        title: '文件大小',
        dataIndex: 'size',
        key: 'size',
      },
      {
        title: '查看结果',
        dataIndex: 'Download',
        key: 'Download',
        render:(text, record)=> {
          return (<Button type="primary"  onClick={()=>this.handleResultData(record)} className={styles.ButtonInfo}>查看结果</Button>);
        },
      },
      {
        title: '下载文件',
        key: 'Download',
        dataIndex: 'Download',
        render:(text, record)=> {
          return (<Button type="primary" icon={<DownloadOutlined/>} onClick={()=>ResultDownload(record)} className={styles.ButtonInfo}>下载文件</Button>);
        },
      },
      {
        title: '删除文件',
        key: 'Delete',
        dataIndex: 'Delete',
        render:(text, record)=> {
          return (
            <Popconfirm
              title="确定要删除此文件吗?"
              onConfirm={() => this.handleDeletea(record)}
              icon={
                <WarningOutlined
                  style={{
                    color: 'red',
                  }}
                />
              }
            >
            <Button type="primary" danger className={styles.ButtonInfo}>删除文件</Button>
            </Popconfirm>
          );
        },
      }
    ];
    //分类结果展示
    const resultcolumns = [
      {
        title: '样本ID',
        dataIndex: 'id',
        width: 150,
      },
      {
        title: '分类',
        dataIndex: 'value',
        width: 150,
      }
    ];
    this.setState({resultlist});
    this.setState({listcolumns});
    this.setState({resultcolumns});
  }

  handleResultData = (record) => {
    console.log(record);
      const { history } = this.props;
      // 跳转到 /list 页面，并携带参数 name = 'example'
      history.push('/model/index?name='+record.name);
    };

  loadData = async () => {
    const resultlist = await Resultlist();
    this.setState({ resultlist });
  }
  handleTableChange = (pagination, filters, sorter) => {
    const { current, pageSize } = pagination;
    this.setState({ currentPage: current, pageSize });
  };
  handleDeletea = (record) => {
    resultDelete(record).then((resp) => {
      if(resp.data=="success"){
        this.loadData();
        message.success("删除文件成功");
      }else {
        message.error("文件删除失败");
      }
    });
  }
  render() {
    const { resultlist, listcolumns,resultcolumns,Resultdata} = this.state;
    const {config}=this.state;
    return (
      <div>
        <Table columns={listcolumns} dataSource={resultlist}></Table>
      </div>
    );
  }
}

export default ExamplePage;

